//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        time: "12:01",
        isAgree: false,
        customItem: '全部',
        productDefault:'',

        product:'',
        get_addr:['河北省', '邢台市', '桥东区'],//购买地址
        addrmany:"2016-09-01",//详细地址
        peisong_time:'',
        tip:1,//小费
        leavemessage:"",//留言
        attrstatus:'',//1帮我买   2 帮我送     3，帮我排队
        addr:'1',//常用收货地址id

        getaddrmany:'',
        gettelphone:'',
        getname:'',
        getaddr:'',
        startTime:'',
        endTime:''
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad: function (options) {
        var date = new Date();
        var startTime = app.util.formatTime(date);
        var endTime = app.util.formatTime(new Date(date.getTime() - 30*24*60*60*1000));
        this.setData({
            productDefault:options.product||'',
            attrstatus:options.attrstatus||'',
            product:options.product||'',
            startTIme:startTime,
            endTime:endTime
        })
    },
    onShow:function(){
        var curr_address = wx.getStorageSync('curr_address');
        if(curr_address){
            var curr_address_json = JSON.parse(curr_address);
            this.setData({
                addr:curr_address_json.a_id,
                getaddrmany:curr_address_json.getaddrmany,
                getaddr:curr_address_json.getaddr,
                gettelphone:curr_address_json.gettelphone,
                getname:curr_address_json.getname,
            })
        }
    },
    submitInfo:function () {
        var that = this;
        if(that.data.isClick) return;
        that.data.isClick = 1;

       if(that.data.o_id){
           wx.showModal({
               title:'提示',
               content:'抱歉，提单失败，请返回首页重新提单',
               showCancle:false,
               success:function (res) {
                   if (res.confirm) {
                       wx.switchTab({
                           url:'/pages/index/index'
                       })
                   }
               }
           })
           that.data.isClick = 0;
           return;
       }
        var reg = /^[1-9]\d{0,2}$/;//最多999元
        var checkTip = reg.test(this.data.tip)
        if(!checkTip){
            wx.showModal({
                title:'提示',
                content:'小费只能输入数字且大于1元！',
                showCancle:false,
            })
            that.data.isClick = 0;
            return;
        }

        if(!that.data.isAgree){
            wx.showModal({
                title:'提示',
                content:'请阅读并同意我们的政策才可以进行下一步操作。',
                showCancle:false,
            })
            that.data.isClick = 0;
            return;
        }
        if(!that.data.product || !that.data.get_addr.length || !that.data.addrmany || !that.data.peisong_time || !that.data.tip || !that.data.addr){
            wx.showModal({
                title:'提示',
                content:'数据不完整，检查后再提交',
                showCancle:false,
            })
            that.data.isClick = 0;
            return;
        }
        var data = {
            product:that.data.product,
            get_addr:that.data.get_addr.join(','),
            addrmany:that.data.addrmany,
            peisong_time:that.data.peisong_time,
            tip:that.data.tip,
            leavemessage:that.data.leavemessage,
            attrstatus:that.data.attrstatus,
            addr:that.data.addr,
        }
        wx.showToast({
            title:'加载中',
            icon:'loading'
        })
        app.util.gotopay(that ,data)
    },
    enabledClick:function(){
        var that = this;
        setTimeout(function () {
            that.data.isClick = 0;
        },1000)
    },
    bindChangeProduct:function(e){
        this.data.product = e.detail.value
    },
    bindChangeRegion: function (e) {
        this.setData({
            get_addr: e.detail.value
        })
    },
    bindChangeAddr:function (e) {
        this.data.addrmany = e.detail.value
    },
    bindDateChange: function (e) {
        this.setData({
            peisong_time: e.detail.value
        })
    },
    bindChangeTips:function (e) {
        // var reg = /^[[1-9]|([0-9][0-9]{0,*})]$/;
        var val =  e.detail.value;
        // if(!reg.test(val)) {
        //     return false;
        // };
        this.data.tip = val
    },
    bindChangeMsg:function (e) {
        this.data.leavemessage = e.detail.value
    },
    bindAgreeChange: function (e) {
        this.setData({
            isAgree: !!e.detail.value.length
        });
    },
    bindSelectAddr:function () {
        var that = this;
        wx.navigateTo({
            url:'/pages/addressList/addressList?returnPage=' + '/pages/publish/publish'
        })
    }

})
